<?php

// +----------------------------------------------------------------------
// | H1CMS © OpenSource CMS
// +----------------------------------------------------------------------
// | Copyright (c) 2014-2016 http://www.h1cms.com All rights reserved.
// | Copyright (c) 2014-2016 嘉兴领格信息技术有限公司，并保留所有权利。
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: Allen <allen@lg4.cn>
// +----------------------------------------------------------------------

namespace Lg\View;

use Lg\Config;

/**
 * Description of Asset
 *
 * @author Allen <allen@lg4.cn>
 */
class Asset {

    const HEAD = 0;
    const BODY_BEGIN = 1;
    const BODY_END = 2;
    const DOM_READY = 3;
    const DOM_LOAD = 4;

    static $staticLibs = [
        'js' => [],
        'css' => []
    ];

    public static function addJs($filename, $pos = Asset::HEAD) {
        static::$staticLibs['js'][$pos][] = $filename;
    }

    public static function addCss($filename, $pos = Asset::HEAD) {
        static::$staticLibs['css'][$pos][] = $filename;
    }

    public static function addJsBlock($block, $pos = Asset::HEAD) {
        static::$staticLibs['jsblock'][$pos][] = $block;
    }

    public static function addCssBlock($block, $pos = Asset::HEAD) {
        static::$staticLibs['cssblock'][$pos][] = $block;
    }

    public static function register($libarayname, $version = NULL) {
        if (empty($version)) {
            $libs = Config::get('static.' . $libarayname);
        } else {
            $libs = Config::get('static.' . $libarayname);
            if (isset($libs[$version])) {
                $libs = $libs[$version];
            }
        }
        if (isset($libs['js'])) {
            foreach ($libs['js'] as $jsFile => $pos) {
                if (is_string($jsFile)) {
                    static::$staticLibs['js'][$pos][] = $jsFile;
                } else {
                    static::$staticLibs['js'][Asset::HEAD][] = $pos;
                }
            }
        }
        if (isset($libs['css'])) {
            foreach ($libs['css'] as $cssFile => $pos) {
                if (is_string($cssFile)) {
                    static::$staticLibs['css'][$pos][] = $cssFile;
                } else {
                    static::$staticLibs['css'][Asset::HEAD][] = $pos;
                }
            }
        }
    }

    public static function render($pos = Asset::HEAD, $outputReturn = false) {
        $buffer = '';
        //render css
        $cssFiles = data_get(static::$staticLibs['css'], $pos, []);
        $jsFiles = data_get(static::$staticLibs['js'], $pos, []);
        foreach ($cssFiles as $css) {
            $buffer .= "<link rel=\"stylesheet\" type=\"text/css\" href=\"" . $css . "\" />\n";
        }

        foreach ($jsFiles as $js) {
            $buffer .= "<script type=\"text/javascript\" src=\"" . $js . "\"></script>\n";
        }

        //block
        $cssBlocks = data_get(static::$staticLibs['cssblock'], $pos, []);
        $jsBlocks = data_get(static::$staticLibs['jsblock'], $pos, []);

        foreach ($cssBlocks as $css) {
            $buffer .= "<style type=\"text/css\">{$css}</style>\n";
        }

        if (!empty($jsBlocks)) {
            $buffer .= "<script type=\"text/javascript\">";
            if ($pos == Asset::DOM_LOAD) {
                $buffer .= "jQuery(document).load(function() {";
            } else if ($pos == Asset::DOM_READY) {
                $buffer .= "jQuery(document).ready(function() {";
            }
            foreach ($jsBlocks as $js) {
                $buffer .= $js;
            }
            if ($pos == Asset::DOM_LOAD) {
                $buffer .= "});";
            } else if ($pos == Asset::DOM_READY) {
                $buffer .= "});";
            }
            $buffer .= "</script>";
        }
        if ($outputReturn) {
            return $buffer;
        }
        echo $buffer;
    }

}
